package cn.sxt.users.service.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

import cn.sxt.common.util.PageInfoUtil;
import cn.sxt.questions.pojo.Columns;
import cn.sxt.users.dao.AdminsDao;
import cn.sxt.users.dao.RoleDao;
import cn.sxt.users.pojo.Admins;
import cn.sxt.users.pojo.Role;
import cn.sxt.users.service.IUserService;

@Service("userService")
public class IUserServiceImpl implements IUserService{

	@Resource
	private AdminsDao adminsDao;
	@Resource
	private RoleDao roleDao;
	
	
	@Override
	public Admins findOneAdminsService(Map<String, Object> condMap) {
		return adminsDao.findOne(condMap);
	}

	@Override
	public JSONObject saveService(Admins admins) {
		JSONObject resultJson=new JSONObject();
		int res=this.adminsDao.save(admins);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admins.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateService(Admins admins) {
		JSONObject resultJson=new JSONObject();
		int res=this.adminsDao.update(admins);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admins.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteService(Admins admins) {
		JSONObject resultJson=new JSONObject();
		int res=this.adminsDao.delete(admins);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", admins.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<Admins> findListAdminsService(Map<String, Object> condMap, PageInfoUtil pageInfoUtil) {
		if(pageInfoUtil!=null){
			Page<Admins> page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<Admins> adminsList=this.adminsDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return adminsList;
		}
		return adminsDao.findList(condMap);
		
	}

	@Override
	public Role findOneRoleService(Map<String, Object> condMap) {
		return this.roleDao.findOne(condMap);	
	}

	@Override
	public JSONObject saveService(Role role) {
		JSONObject resultJson=new JSONObject();
		int res=this.roleDao.save(role);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", role.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject updateService(Role role) {
		JSONObject resultJson=new JSONObject();
		int res=this.roleDao.update(role);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", role.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public JSONObject deleteService(Role role) {
		JSONObject resultJson=new JSONObject();
		int res=this.roleDao.delete(role);
		if(res>0){
			JSONObject dataJson=new JSONObject();
			dataJson.put("id", role.getId()+"");
			dataJson.put("effectCount",res);
			resultJson.put("code", "0");
			resultJson.put("info", "成功");
			resultJson.put("data", dataJson);
		}else{
			resultJson.put("code", "1");
			resultJson.put("info", "失败");
		}
		return resultJson;
	}

	@Override
	public List<Role> findListRoleService(Map<String, Object> condMap, PageInfoUtil pageInfoUtil) {
		if(pageInfoUtil!=null){
			Page<Role> page=PageHelper.startPage(pageInfoUtil.getCurrentPage(),pageInfoUtil.getPageSize());
			List<Role> roleList=this.roleDao.findList(condMap);
			pageInfoUtil.setTotalRecord(Integer.valueOf(page.getTotal()+""));
			return roleList;
		}
		return roleDao.findList(condMap);
	}

	/**
	 * 
	 */


}
